<?php
class InforsController extends AppController {

	var $name = 'Infors';
	var $uses = array('Member','Youcart','Product','InvoicesDetail','Invoice');
	
	function index() {
	   $this->set('namesOfLevel', $this->Tree->namesOfLevel);
		//pr($this->Tree->abc);die();
        $id_user=$this->Session->read("tkid");
        
        // Lay tong thanh tich toan cong ty gom ca qua khu
        $histories = $this->History->find('all',
                array('conditions' =>
                    array("History.month <> '00-0000'",
                    )
        ));
        $totalPv = 0;
        foreach($histories as $history){
            $data = json_decode($history['History']['data'], true);
            $totalPv += $data['totalPv'];
        }
        $info = $this->Session->read('info');
        $this->set('totalPv', $info['totalPv'] + $totalPv);
        
        
		//lay thang 
		if (!empty($this->data['month'])){
		    $history = $this->History->findByMonth($this->data['month']);
		    $data = json_decode($history['History']['data'], true);
		    $this->set('member', $data[$id_user]);
		} else {
		  $this->set('member', $info[$id_user]);
		  $this->data=$this->Member->read(null,$id_user);
		 // pr($this->data);die();

		}

		if (is_array($this->data)){
		    $this->data = array_merge($this->data, $this->Member->read(null,$id_user));
		} else {
		    $this->data = $this->Member->read(null,$id_user);
		}
	}
	//tinh day vao mang 
	function addcarts($id=null) {
		
		$id_user=$this->Session->read("tkid");
		//pr($id_user);die();
		$sp=$this->Product->read(null,$id);
		$ip=getenv("REMOTE_ADDR");
		$ck=$this->Youcart->find('count',
										 array('conditions' =>
										 		 array('Youcart.user_id' => $id_user,
														'Youcart.session_you'=>$this->Session->id(),
														'Youcart.ip_you'=>$ip
												)
												));
		if($ck==0){
		$car=array('Youcart'=>array(
						'user_id'=>$id_user,
						'id_product'=>$id,
						'name_product'=>$sp['Product']['name'],
						'code_product'=>$sp['Product']['code'],
						'cost_product'=>$sp['Product']['cost'],
						'alias'=>$sp['Product']['alias'],
						'ip_you'=>$ip,
						'session_you'=>$this->Session->id()
						)
		
				);
			if($this->Youcart->save($car)){
				$this->redirect('carts');
				
			}
		}else{
			$test=$this->Youcart->find('all',array(
												'conditions'=>array(
													'Youcart.user_id'=>$id_user,
													'Youcart.ip_you'=>$ip,
													'Youcart.id_product'=>$id,
													'Youcart.session_you'=>$this->Session->id(),
												),
												'limit'=>'1'
															
										));
				
							
			
			if($test){
					$car=array('Youcart'=>array(
						'id'=>$test[0]['Youcart']['id'],
						'quantity_product'=>$test[0]['Youcart']['quantity_product']+1 
						
						)
		
				);
				//pr($car);die();
				if($this->Youcart->save($car)){
					$this->redirect('carts');
					
				}
			}
			else{
				$car=array('Youcart'=>array(
						'user_id'=>$id_user,
						'id_product'=>$id,
						'name_product'=>$sp['Product']['name'],
						'code_product'=>$sp['Product']['code'],
						'cost_product'=>$sp['Product']['cost'],
						'alias'=>$sp['Product']['alias'],
						'ip_you'=>$ip,
						'session_you'=>$this->Session->id()
						)
		
				);
			if($this->Youcart->save($car)){
				$this->redirect('carts');
				
			}
			}
		}
		
				

	}
	// --------------------------------//
	function carts(){													
		$date=date('Y-m-d');
		//lay tat ca cac san pham khong fai ngay hien tai
		$delete=$this->Youcart->find('all',array('conditions'=>array('Youcart.created <>'=>$date)));
		if($delete){
			foreach($delete as $key){
				$this->Youcart->delete($key['Youcart']['id']);
			}
		}		
		$id_user=$this->Session->read("tkid");
		$ip=getenv("REMOTE_ADDR");
		$shows=$this->Youcart->find('all',array('conditions'=>array(
														'Youcart.user_id'=>$id_user,
														'Youcart.session_you'=>$this->Session->id(),
														'Youcart.ip_you'=>$ip
														)));
		$this->set('shows',$shows);	
		/*												
		if(empty($shows)){
			echo "<script>alert('".json_encode('Bạn hãy mua một sản phẩm !')."');</script>";
			echo "<script>location.href='".System."/infors'</script>";
		}else{												
			$this->set('shows',$shows);	
		}*/
											
	}
	// ---------------------------//
	function updatecarts() {
		
		$ip=getenv("REMOTE_ADDR");
		
		if(!empty($_POST['check']) && isset($_POST['check'])){
			foreach($_POST['check'] as $ID){				
				$this->Youcart->delete($ID);	
			}
			$this->Session->setFlash(__('Bạn Delete thành công', true));
			$name=$_SERVER['HTTP_REFERER'];
			$this->redirect($name);
			
		}
			
		foreach($this->data['Youcart'] as $SLSP){
				$a[]=$SLSP;
		}
		foreach($this->data['ID'] as $IDSP){
			 $b[]=$IDSP;
		}
		$arr_tonghop=array();
		foreach($a as $key=>$ar){
			$arr_tg=array();
			 if(isset($b[$key])){
			  $arr_tg[0]=$a[$key];
			  $arr_tg[1]=$b[$key];
			  $arr_tonghop[]=$arr_tg;
			 } 
			}
			foreach($arr_tonghop as $key){
				$save=array('Youcart'=>array(
						'id'=>$key[1],
						'quantity_product'=>$key[0]
					)); 
				$this->Youcart->save($save);
			} 
			$this->redirect('carts');
			 
	}
	// -------------------------//
 
	//thong  tin kinh doanh
	function business() {
		 
		$id_user=$this->Session->read("tkid");
		//lay hoa dong oflien
		//$this->set('offnile',$this->Invoice->find('all',array('conditions'=>array('Invoice.id_user'=>$id_user),'order'=>'Invoice.id DESC')));
		
		$this->InvoicesDetail->recursive = 0;
		$conditions=array('Invoice.id_user'=>$id_user);
		$this->paginate = array('conditions' =>$conditions,'order'=>'Invoice.id DESC','limit' =>20);
        $business = $this->paginate('Invoice');
		$this->set('business',$business);
		

	}

	function maps() {
		
		$userId = $this->Session->read("tkid");
		$info = $this->Session->read('info');
		$this->Tree->nodes = $info;
		$this->set('viewTree', $this->Tree->printTree($this->Tree->nodes[$userId]));
		

		$id_user=$this->Session->read("tkid");
		$members=$this->Member->find('all',array('conditions'=>array('Member.parent_id'=>$id_user)));
		$this->set('members',$members);
		
	}
	function Profile() {
		
		$id_user=$this->Session->read("tkid");
		$this->data=$this->Member->read(null,$this->Session->read("tkid"));			
	}
	function edit() {
		
		if (!empty($this->data)) {
			if ($this->Member->save($this->data)) {
				$this->Session->setFlash(__('Sửa thành công', true));
				$this->redirect(array('action' => 'profile'));
			} else {
				$this->Session->setFlash(__('Không sửa được vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data=$this->Member->read(null,$this->Session->read("tkid"));
		}			
	}
	function changepass() {
		
			if (!empty($_POST)) {
				// check pass cu co dung khong
				$this->data=$this->Member->read(null,$this->Session->read("tkid"));
				if($this->data['Member']['pass']== md5($_POST['oldpass'])){
						$member=array('Member'=>array(
														'id'=>$this->Session->read("tkid"),
														'pass'=>md5($_POST['newpass'])
														));
					if ($this->Member->save($member)) {
						echo "<script>alert('".json_encode('Đổi mật khẩu thành công !')."');</script>";
						echo "<script>location.href='".System."/infors/profile'</script>";
					}
				}else{
					echo "<script>alert('".json_encode('Mật khẩu cũ không đúng vui lòng nhập lại !')."');</script>";
					echo "<script>history.back(-1);</script>";
				
				}
				
			
		}
		if (empty($this->data)) {
			$this->data=$this->Member->read(null,$this->Session->read("tkid"));
		}	

	}
	
}
?>